/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.event.boundary;

import com.aljfce.business.calendar.control.CalendarManager;
import com.aljfce.business.entity.Calendar;
import com.aljfce.business.entity.Event;
import com.aljfce.business.entity.Notification;
import com.aljfce.business.entity.User;
import com.aljfce.business.notification.boundary.NotificationBean;
import com.aljfce.business.notification.control.NotificationManager;
import com.aljfce.business.notification.exception.NotificationNotFoundException;
import com.aljfce.business.profile.control.ProfileManager;
import com.aljfce.business.profile.exception.UserNotFoundException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Jacopo Ferrigno <jacopo.ferrigno@gmail.com>
 */
@ManagedBean
@ViewScoped
public class InvitedEvent {

    @Inject
    ProfileManager pm;

    @Inject
    CalendarManager cm;

    @PersistenceContext
    EntityManager em;

    @Inject
    NotificationManager nm;

    HttpServletRequest request;
    Principal principal/*request.getUserPrincipal()*/;
    FacesContext context;

    ArrayList<Event> myEvents;
    Event event;

    public ArrayList<Event> getMyEvents() {
        return myEvents;
    }

    public void setMyEvents(ArrayList<Event> myEvents) {
        this.myEvents = myEvents;
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event Event) {
        this.event = Event;
    }

    private User user;

    /**
     * Get the value of user
     *
     * @return the value of user
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the value of user
     *
     * @param user new value of user
     */
    public void setUser(User user) {
        this.user = user;
    }

    private Notification notification;

    /**
     * Get the value of notification
     *
     * @return the value of notification
     */
    public Notification getNotification() {
        return notification;
    }

    /**
     * Set the value of notification
     *
     * @param notification new value of notification
     */
    public void setNotification(Notification notification) {
        this.notification = notification;
    }

    private ArrayList<Event> eventsOverlapping;

    public ArrayList<Event> getEventsOverlapping() {
        return eventsOverlapping;
    }

    public void setEventsOverlapping(ArrayList<Event> eventsOverlapping) {
        this.eventsOverlapping = eventsOverlapping;
    }

    private ArrayList<Event> invitedEvents;

    public ArrayList<Event> getInvitedEvents() {
        return invitedEvents;
    }

    public void setInvitedEvents(ArrayList<Event> invitedEvents) {
        this.invitedEvents = invitedEvents;
    }

    @PostConstruct
    public void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        principal = request.getUserPrincipal();
        myEvents = new ArrayList<>();
        eventsOverlapping = new ArrayList<>();

        try {
            user = pm.getProfile(principal.getName());
        } catch (UserNotFoundException ex) {
            Logger.getLogger(InvitedEvent.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Associates the <i>event</i> to be accepted by a {@link User} to his
     * {@link Calendar}, and deletes the {@link Notification} <b>Invitation</b>
     * containing this <i>event</i> and related to this user
     *
     * @param event to accept
     */
    public void accept(Event event) {
        //FIXME do something when this call fails
        try {
            user = pm.getProfile(principal.getName());
        } catch (UserNotFoundException ex) {
            Logger.getLogger(InvitedEvent.class.getName()).log(Level.SEVERE, null, ex);
        }
        cm.addEvent(event, user.getEmail());

        try {
            nm.deleteNotification(notification);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(InvitedEvent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotificationNotFoundException ex) {
            Logger.getLogger(InvitedEvent.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Deletes the {@link Notification} <b>Invitation</b>
     * containing this <i>event</i> and related to a user
     *
     * @param event To decline
     */
    public void decline(Event event) {
        //TODO write real implementation
        user.getNotificationCollection().remove(notification);
    }

    /**
     *
     * @param event To check the overlap
     * @return the set of events overlapping with the <i>event</i> in parameter
     * in calendar where the events returned are in.
     */
    public ArrayList<Event> showOverlapping(Event event) {
        //FIXME do something when this call fails
        return cm.checkOverlapping(user, event);
    }

    public void showInvitedEvent() {
        //TODO write real implementation

    }
}
